package net.cassite.daf4j.ds;

import net.cassite.daf4j.*;

/**
 * 表达式Resolver
 *
 * @param <Context>      上下文
 * @param <BasicElement> 基本元素
 */
public class ExpressionResolver<Context, BasicElement> {
        private final ExpressionParser<Context, BasicElement> expressionParser;

        public ExpressionResolver(ExpressionParser<Context, BasicElement> expressionParser) {
                this.expressionParser = expressionParser;
        }

        @SuppressWarnings("unchecked")
        public BasicElement resolve(Context context, IExpression exp) throws Exception {
                Object[] argArr = exp.expArgs();
                if (exp.expType() == ExpressionTypes.add) {
                        return expressionParser.add(context, argArr[0], argArr[1]);
                } else if (exp.expType() == ExpressionTypes.avg) {
                        return expressionParser.avg(context, (DataComparable<?>) argArr[0]);
                } else if (exp.expType() == ExpressionTypes.concat) {
                        if (argArr[0] instanceof IData && argArr[1] instanceof String) {
                                return expressionParser.concat(context, (IData<String>) argArr[0], (String) argArr[1]);
                        } else if (argArr[0] instanceof IData && argArr[1] instanceof IData) {
                                return expressionParser.concat(context, (IData<String>) argArr[0], (IData<String>) argArr[1]);
                        } else if (argArr[0] instanceof String && argArr[1] instanceof IData) {
                                return expressionParser.concat(context, (String) argArr[0], (IData<String>) argArr[1]);
                        } else {
                                throw new IllegalArgumentException();
                        }
                } else if (exp.expType() == ExpressionTypes.count) {
                        if (argArr[0] instanceof DataComparable) {
                                return expressionParser.count(context, (DataComparable<?>) argArr[0]);
                        } else {
                                return expressionParser.count(context, argArr[0]);
                        }
                } else if (exp.expType() == ExpressionTypes.divide) {
                        return expressionParser.divide(context, argArr[0], argArr[1]);
                } else if (exp.expType() == ExpressionTypes.exists) {
                        return expressionParser.exists(context, (PreResult<?>) argArr[0]);
                } else if (exp.expType() == ExpressionTypes.length) {
                        return expressionParser.length(context, (IData<String>) argArr[0]);
                } else if (exp.expType() == ExpressionTypes.locate) {
                        if (argArr[0] instanceof IData && argArr[1] instanceof String) {
                                return expressionParser.locate(context, (IData<String>) argArr[0], (String) argArr[1]);
                        } else if (argArr[0] instanceof IData && argArr[1] instanceof IData) {
                                return expressionParser.locate(context, (IData<String>) argArr[0], (IData<String>) argArr[1]);
                        } else if (argArr[0] instanceof String && argArr[1] instanceof IData) {
                                return expressionParser.locate(context, (String) argArr[0], (IData<String>) argArr[1]);
                        } else {
                                throw new IllegalArgumentException();
                        }
                } else if (exp.expType() == ExpressionTypes.lower) {
                        return expressionParser.lower(context, (IData<String>) argArr[0]);
                } else if (exp.expType() == ExpressionTypes.max) {
                        return expressionParser.max(context, (DataComparable<?>) argArr[0]);
                } else if (exp.expType() == ExpressionTypes.min) {
                        return expressionParser.min(context, (DataComparable<?>) argArr[0]);
                } else if (exp.expType() == ExpressionTypes.minus) {
                        return expressionParser.minus(context, argArr[0], argArr[1]);
                } else if (exp.expType() == ExpressionTypes.mod) {
                        return expressionParser.mod(context, argArr[0], argArr[1]);
                } else if (exp.expType() == ExpressionTypes.multi) {
                        return expressionParser.multi(context, argArr[0], argArr[1]);
                } else if (exp.expType() == ExpressionTypes.notExists) {
                        return expressionParser.notExists(context, (PreResult<?>) argArr[0]);
                } else if (exp.expType() == ExpressionTypes.substring) {
                        return expressionParser.substring(context, (IData<String>) argArr[0], (Integer) argArr[1], (Integer) argArr[2]);
                } else if (exp.expType() == ExpressionTypes.sum) {
                        return expressionParser.sum(context, (DataComparable<?>) argArr[0]);
                } else if (exp.expType() == ExpressionTypes.trim) {
                        return expressionParser.trim(context, (IData<String>) argArr[0]);
                } else if (exp.expType() == ExpressionTypes.unary_negative) {
                        return expressionParser.unary_negative(context, (ParameterComparable<?>) argArr[0]);
                } else if (exp.expType() == ExpressionTypes.upper) {
                        return expressionParser.upper(context, (IData<String>) argArr[0]);
                } else {
                        throw new IllegalArgumentException();
                }
        }
}
